import math
import matplotlib.pyplot as plt
= [i * 0.1 for i in range(-63, 64)]
x = [math.sin(xi) for xi in x]
y
plt.plot(x, y,)'Plot of y = sin(x)')
plt.title('x')
plt.xlabel('sin(x)')
plt.ylabel( plt.show()
What is the Taylor series?
The Taylor series is a mathematical representation of a function as an infinite sum of terms. It is mainly used to approximate a non-polynomial function in terms of polynomials of degree n. Higher the degree n, the better the approximation.
The general form of the Taylor series for the function \(f(x)\) centered about the point a is given by:
\[ f(x) = f(a) + f'(a)(x-a) + \frac{f''(a)(x-a)^2}{2!} + \frac{f'''(a)(x-a)^3}{3!} +... \]
Let’s say we take the function \(y = sin(x)\)
Let’s choose a=0 and let’s go up to n=5, then using the Taylor series we can write,
\[ sin(x) = 0 + cos(0)(x)+\frac{-sin(0)x^2}{2!}+\frac{-cos(0)x^3}{3!}+\frac{sin(0)x^4}{4!}+\frac{cos(0)x^5}{5!} \]
where \(f(x)=sin(x),f'(x)=cos(x),f''(x)=-sin(x),f'''(x)=-cos(x),f''''(x)=sin(x),and f'''''(x)=cos(x)\)
Therefore,
\(sin(x) = x-\frac{x^3}{3!}+\frac{x^5}{5!}\)
import math
import matplotlib.pyplot as plt
= [i * 0.1 for i in range(-33, 34)]
x = [xi-((xi**3)/6)+((xi**5)/120) for xi in x]
y = [math.sin(xi) for xi in x]
y1
plt.figure()="y=x-(x^3)/3!+(x^5)/5!")
plt.plot(x, y,label="y=sinx")
plt.plot(x,y1,label'Approximating sin(x) using y = x - (x^3)/3! + (x^5)/5!')
plt.title('x')
plt.xlabel('y')
plt.ylabel(="upper left")
plt.legend(loc plt.show()
We can see above that, the polynomial function is providing us a good approximation of the sine function but only for values close to zero (as we selected a to be 0). For other values the approximation is inaccurate.
When we truncate the infinite Taylor series to a polynomial of degree n, we call it the Taylor polynomial of degree n. The higher the value of n, the more accurate our approximation is.
Taylor series for bivariate functions
We can generalize the Taylor series for functions depending on multiple variables.
Let’s discuss the Taylor series for bivariate functions. The formula is given by,
\[ f(x,y)= f(a,b)+\frac{\partial f}{\partial x}(x-a)+\frac{\partial f}{\partial y}(y-b)+\frac{\partial^2 f}{\partial x^2}\frac{(x-a)^2}{2!}+\frac{\partial^2 f}{\partial y^2}\frac{(y-b)^2}{2!}+... \]
Here the approximation of the function takes place near the point,\((a,b)\), and the partial derivatives are calculated at the point \((a,b)\).
Let’s take an example.
\[ f(x,y)=x^2+y^2 \]
We can try to approximate this function using the 2D Taylor series around the point \((1,1)\) and let’s choose the value of n as 2.
Let’s first plot \(f(x,y)\).
import numpy as np
import matplotlib.pyplot as plt
= np.linspace(-5,5,100)
x1 = np.linspace(-5,5,100)
y1 =np.meshgrid(x1,y1)
X1,Y1 =X1**2 + Y1**2
Z_true
= plt.figure()
fig = plt.axes(projection="3d")
ax ="plasma")
ax.plot_surface(X1,Y1,Z_true,cmap"X")
ax.set_xlabel("Y")
ax.set_ylabel("Z")
ax.set_zlabel(=20, azim=30)
ax.view_init(elev plt.show()
Let us try to approximate this function using the Taylor series with a=b=1 and n =2.
\[ \begin{equation} \begin{split} f(x,y)&=2+2(x-1)+2(y-1)+(x-1)^2+(y-1)^2\\ f(x,y)&=x^2+y^2 \end{split} \end{equation} \]
which is the true value indeed. Hence, the Taylor series exactly approximates \(f(x,y)\) when centered about the point \((1,1)\).
You can visualize the approximations of the Taylor series in the link given below.